package com.delayqueue.master.netty.handler.inbound;

import io.netty.handler.ipfilter.IpFilterRule;
import io.netty.handler.ipfilter.IpFilterRuleType;
import lombok.extern.slf4j.Slf4j;

import java.net.InetSocketAddress;
import java.util.Set;


@Slf4j
public class IpFilterHandler implements IpFilterRule {

    private Set<String> ipWhiteList;

    public IpFilterHandler(Set<String> ipWhiteList) {
        this.ipWhiteList = ipWhiteList;
    }

    @Override
    public boolean matches(InetSocketAddress inetSocketAddress) {
        String ip = inetSocketAddress.getAddress().getHostAddress();
        return !ipWhiteList.contains(ip);
    }

    @Override
    public IpFilterRuleType ruleType() {
        return IpFilterRuleType.REJECT;
    }
}
